package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Jgxm;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.JgxmMapper;
import com.gfxy.modules.items.service.IJgxmService;
import com.gfxy.modules.items.vo.*;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 教改项目
 * @Author: jeecg-boot
 * @Date:   2021-10-03
 * @Version: V1.0
 */
@Service
public class JgxmServiceImpl extends AbstractProjectServiceImpl<JgxmMapper, Jgxm> implements IJgxmService {
	protected int memberCellNum = 5;
//
//
//	@Override
//	public List<String> importExcel(MultipartFile file) throws IOException {
//		List<String> result = new ArrayList<>();
//		InputStream inputStream = file.getInputStream();
//		if (!(inputStream).markSupported()) {
//			inputStream = new PushbackInputStream(inputStream, 8);
//		}
//
//		Workbook book = WorkbookFactory.create(inputStream);
//		Sheet sheet = book.getSheetAt(0);
//
//		List<JgxmPage> list = new ArrayList<>();
//
//		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
//			Row row = sheet.getRow(i);
//
//			String year = ExcelUtil.formatCell(row.getCell(0));
//			String classess = ExcelUtil.formatCell(row.getCell(1));
//			String award = ExcelUtil.formatCell(row.getCell(2));
//			String projectName = ExcelUtil.formatCell(row.getCell(3));
//
//			String members = ExcelUtil.formatCell(row.getCell(4));
//			String[] memberArray = members.split("、");
//
//			JgxmPage jgxm = new JgxmPage();
//			jgxm.setYear(year);
//			jgxm.setClassess(translateClassess(classess));
//			jgxm.setAward(translateAward(award));
//			jgxm.setProjectName(projectName);
//			jgxm.setIsPass(0);
//
//			List<MemberList> memberListList = new ArrayList<>();
//			for (int j = 0; j < memberArray.length; j++) {
//				String memberStr = memberArray[j];
//				List<MemberList> memberLists = memberListMapper.selectByRealName(memberStr.trim());
////				if(memberLists.size()==1){
////
////					MemberList memberList = memberLists.get(0);
////					memberList.setRanked(j+1);
////					memberList.setProjectType("20");
////					memberList.setIsCustom("0");
////					memberListList.add(memberList);
////
////				} else if(memberLists.size() == 0){
////
////					result.add(jgxm.getProjectName() +  "中，教师:" + memberStr + " 数据不存在，请手动处理");
////					MemberList memberList = new MemberList();
////					memberList.setRanked(j+1);
////					memberList.setProjectType("20");
////					memberList.setIsCustom("0");
////					memberListList.add(memberList);
////				} else if(memberLists.size()>1){
////
////					result.add(jgxm.getProjectName() +  "中，教师:" + memberStr + " 存在同名教师，请手动处理");
////					MemberList memberList = new MemberList();
////					memberList.setRanked(j+1);
////					memberList.setProjectType("20");
////					memberList.setIsCustom("0");
////					memberListList.add(memberList);
////				}
//			}
//
//			double totalScore = this.getTotalScore(jgxm.getClassess(),jgxm.getAward());
//			jgxm.setTotalScore(totalScore);
//			ExcelUtil.computeScore(memberListList,totalScore);
//
//			jgxm.setMemberListList(memberListList);
//			list.add(jgxm);
//		}
//
//		for (JgxmPage page : list) {
//			Jgxm po = new Jgxm();
//			BeanUtils.copyProperties(page, po);
//			this.saveMain(po, page.getMemberListList());
//		}
//
//		return result;
//	}

	private int translateClassess(String classess){
		if(classess == null || classess.equals("")){
			return 0;
		}else if(classess.equals("省部级")){
			return 2;
		} else if(classess.equals("市厅级")){
			return 3;
		}  else if(classess.equals("校级")){
			return 4;
		}  else if(classess.equals("协会项目")){
			return 5;
		} else {
			return 0;
		}
	}
	private int translateStep(String step){
		if(step == null || step.equals("")){
			return 0;
		}else if(step.equals("立项")){
			return 1;
		} else if(step.equals("结项")){
			return 2;
		} else if (step.equals("立项、结题")){
			return 3;
		} else {
			return 0;
		}
	}

	private int translateAward(String award){
		if(award == null || award.equals("")){
			return 0;
		} else if(award.equals("重点项目")){
			return 1;
		} else if(award.equals("一般项目")){
			return 2;
		} else if(award.equals("国家级协会、学会项目")){
			return 1;
		} else if(award.equals("省级协会、学会项目")){
			return 2;
		} else {
			return 0;
		}
	}

	@Override
	protected void pass(String id, String isPass) {

	}

	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}
	@Override
	protected Jgxm getOneProject(Row row) {
		String year = ExcelUtil.formatCell(row.getCell(0));
		String classessStr = ExcelUtil.formatCell(row.getCell(1));
		String award = ExcelUtil.formatCell(row.getCell(2));
		String step = ExcelUtil.formatCell(row.getCell(3));
		String projectName = ExcelUtil.formatCell(row.getCell(4));
		Jgxm jxcg = new Jgxm();
		jxcg.setDate(year);
		jxcg.setClassess(this.translateClassess(classessStr));
		jxcg.setAward(this.translateAward(award));
		jxcg.setStep(this.translateStep(step));
		jxcg.setProjectName(projectName);
		jxcg.setIsPass(0);
		jxcg.setTotalScore(this.getTotalScore(jxcg));
		System.out.println(jxcg);
		return jxcg;
	}

	@Override
	public Double getTotalScore(Jgxm jgxm) {
		double result = 0;
		Integer classess = jgxm.getClassess();
		Integer award = jgxm.getAward();
		Integer step = jgxm.getStep();
		if(classess == 2){
			if(award == 1){
				result = 80;
			}else if (award == 2) {
			    result = 50;
			}
		} else if(classess == 3 ) {
			if(award == 1){
				result = 30;
			}else if (award == 2) {
				result = 20;
			}
		} else if(classess == 4 ) {
			if (award == 1){
				result = 10;
			}else if (award == 2) {
			    result = 5;
			}
		} else if(classess == 5 ) {
			if(award == 1){
				result = 20;
			}else if (award == 2) {
				result = 10;
			}
		}

		double value = new BigDecimal(result/2).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
		if (step == 1){
			result = value;
		}else if (step == 2){
			result -= value;
		}
		return result;
	}

	@Override
	public Page<JgxmMemberPage> selectListPage(String date, int pageNum, int pageSize) {
		Page<JgxmMemberPage> jgxmMemberPagePage = new Page<>(pageNum, pageSize);
		Page<JgxmMemberPage> jgxmMemberPagePage1 = jgxmMemberPagePage.setRecords(baseMapper.selectJgxmMemberPageListByYear(date));
		return jgxmMemberPagePage1;
	}

	@Override
	public List<JgxmMemberPage> getMemberPageListByYear(String date) {
		return baseMapper.selectJgxmMemberPageListByYear(date);
	}


	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Jgxm jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getClassess());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(JgxmMemberPage.class, "classess", String.valueOf(jxcg.getClassess())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		//奖项
		TeachingResultContent awardTeachingResultContent = new TeachingResultContent();
		awardTeachingResultContent.setType(1);
		awardTeachingResultContent.setLevel(jxcg.getAward());
		try {
			awardTeachingResultContent.setValue(dictTranslater.getDictValue(JgxmMemberPage.class, "award", String.valueOf(jxcg.getAward())));
			result.add(awardTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		//奖项
		TeachingResultContent stepTeachingResultContent = new TeachingResultContent();
		stepTeachingResultContent.setType(1);
		stepTeachingResultContent.setLevel(jxcg.getAward());
		try {
			stepTeachingResultContent.setValue(dictTranslater.getDictValue(JgxmMemberPage.class, "step", String.valueOf(jxcg.getStep())));
			result.add(stepTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		return result;
	}
//	@Override
//	public Double[] getMemberListScore(double totalScore, int memberNum) {
//		return new Double[0];
//	}


//
//	@Override
//	public double getTotalScore(Integer classess,Integer award){
//		double result = 0;
//		if(classess == 2){
//			if(award == 1){
//				result = 80;
//			}else if (award == 2) {
//			    result = 50;
//			}
//		} else if(classess == 3 ) {
//			if(award == 1){
//				result = 30;
//			}else if (award == 2) {
//				result = 20;
//			}
//		} else if(classess == 4 ) {
//			if (award == 5){
//				result = 10;
//			}else if (award == 6) {
//			    result = 5;
//			}
//		} else if(classess == 5 ) {
//			if(award == 3){
//				result = 20;
//			}else if (award == 4) {
//				result = 10;
//			}
//		}
//		return result;
//	}

}
